<?php
    //require section
    require_once dirname(dirname(__FILE__)) . '/shared/config/config.php';
    require_once DOCUMENT_ROOT . DIR_INCLUDE . '/check_user_login.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_API . '/api_document.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . '/lib_pager.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . '/lib_upload.php';
    $title = 'Admin Panel';

    $id = 0;
    $name = "";
    $detail = "";
    $cat_id = "";
    $price = "";
    $image = "";
    $file = "";
    $action = "Add";
    $action_show = "Thêm";

    if (!empty($_GET['Action']) && $_GET['Action'] === 'Edit') {
        if (!empty($_GET['Id'])) {
            $id = $_GET['Id'];
        }
        $edit_item   = api_document::get_document_by_id($id);
        $id          = $edit_item['doc_id'];
        $name        = $edit_item['doc_name'];
        $detail      = $edit_item['doc_detail'];
        $cat_id      = $edit_item['doc_cat_id'];
        $price       = $edit_item['doc_price'];
        $image       = $edit_item['doc_img_url'];
        $file        = $edit_item['doc_file'];
        $action      = "Update";
        $action_show = "Sửa";
    } else if (!empty($_POST)) {
        $id_save = "";
        if (!empty($_POST['Id'])) {
            $id_save = $_POST['Id'];
        }
        $name_save = "";
        if (!empty($_POST['txt_name'])) {
            $name_save = $_POST['txt_name'];
        }
        $detail_save = "";
        if (!empty($_POST['txt_detail'])) {
            $detail_save = $_POST['txt_detail'];
        }
        $cat_id_save = "";
        if (!empty($_POST['ddl_cat_id'])) {
            $cat_id_save = $_POST['ddl_cat_id'];
        }

        $price_save = "";
        if (!empty($_POST['txt_price'])) {
            $price_save = $_POST['txt_price'];
        }
        $image_save = "";
        if (!empty($_FILES['f_image']['size'])) {
            $lib_upload = new lib_upload();
            if ($lib_upload->upload_file('f_image', DOCUMENT_ROOT . DIR_SHARED_UPLOAD_IMAGES_DOCUMENT)) {
                $image_save = $_FILES['f_image']['name'];
            }
        } else if (!empty($_POST['hidd_image'])) {
            $image_save = $_POST['hidd_image'];
        }
        $file_save = "";
        if (!empty($_FILES['f_file']['size'])) {
            $lib_upload = new lib_upload();
            if ($lib_upload->upload_file('f_file', DOCUMENT_ROOT . DIR_SHARED_UPLOAD_DOCUMENTS)) {
                $file_save = $_FILES['f_file']['name'];
            }
        } else if (!empty($_POST['hidd_file'])) {
            $file_save = $_POST['hidd_file'];
        }

        if (!empty($_POST['Action']) && ($_POST['Action'] === "Add" || $_POST['Action'] === "Update")) {

            $error_show = api_document::validate_document_fields($name_save, $detail_save, $cat_id_save, $price_save,
                $image_save, $file_save);

            if (empty($error_show)) {
                if (api_document::save_document($id_save, $name_save, $detail_save, $cat_id_save, $price_save, $image_save, $file_save)) {
                    $success_info = "Thêm tài liệu &lt;" . $name_save . "&gt; thành công.";
                    if ($id_save > 0) {
                        $success_info = "Sửa tài liệu &lt;" . $name_save . "&gt; thành công.";
                    }
                }
            } else {
                $id          = $id_save;
                $name        = $name_save;
                $detail      = $detail_save;
                $cat_id      = $cat_id_save;
                $price       = $price_save;
                $image       = $image_save;
                $file        = $file_save;
                $action      = $_POST['Action'];
                $action_show = "Sửa";
                if ($action === "Add") {
                    $action_show = "Thêm";
                }
            }
        } else if (!empty($_POST['Action']) && $_POST['Action'] === "Delete") {
            $id_delete = 0;
            if (!empty($_POST['delete_id'])) {
                $id_delete = $_POST['delete_id'];
            }
            api_document::delete_document($id_delete);
            $success_info = "Xóa tài liệu thành công";
        }
    }



?>

<!DOCTYPE html>
<html>
<head>
    <?php
        require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'head.php';
    ?>
</head>
<body>
<div class="uk-container uk-container-center uk-margin-top">
    <?php
        require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'menu.php';
    ?>

    <section class="ts-body-container uk-panel uk-panel-box ts-padding-large">
        <form action="?" method="POST" id="form_delete">
            <input type="hidden" name="delete_id" value="0"/>
            <input type="hidden" name="Action" value="Delete"/>
        </form>
        <form class="uk-form uk-width-medium-1-1" name="form_data" id="form_data" method="POST"
              action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" enctype="multipart/form-data">
            <?php
                if (!empty($error_show)) {
                    ?>
                    <div class="error">Lỗi: <?php echo $error_show; ?></div>
                <?php
                } else if (!empty($success_info)) {
                    ?>
                    <div class="success"><?php echo $success_info; ?></div>
                <?php
                }?>
            <fieldset>
                <legend><h2>Bài tập</h2></legend>

                <div class="ui form segment form-background">
                    <input type="hidden" name="Id" value="<?php echo $id; ?>"/>

                    <div class="uk-form-row">
                        <label for="Title">Tên tài liệu <span class="required">*</span></label>
                        <input id="Title" name="txt_name" value="<?php echo $name ?>" placeholder="" type="text">
                    </div>

                    <div class="uk-form-row">
                        <label for="Description">Loại tài liệu</label>
                        <select name="ddl_cat_id">
                            <?php
                                $list = api_document::get_all_document_categories();

                                foreach ($list as $i => $item) {
                                    ?>
                                    <option value="<?php echo $item['cat_id'] ?>"
                                        <?php
                                            if (!empty($edit_article_old)) {

                                                if ($item['cat_id'] == $cat_id) {
                                                    echo 'selected';
                                                }
                                            }
                                        ?>
                                        >
                                        <?php echo $item['cat_name'] ?> </option>
                                <?php

                                }
                            ?>
                        </select>
                    </div>

                    <div class="uk-form-row">
                        <label for="Description">Chi tiết <span class="required">*</span></label>
                        <textarea id="Description" name="txt_detail" placeholder="Detail" class="form-control"
                                  rows="3"><?php echo $detail; ?></textarea>
                    </div>

                    <div class="uk-form-row">
                        <label for="Title">Giá <span class="required">*</span></label>
                        <input id="Title" name="txt_price" value="<?php echo $price ?>" placeholder="" type="text">
                    </div>

                    <div class="uk-form-row">
                        <label for="Poster">Hình ảnh <span class="required">*</span></label>

                        <input type="file" name="f_image"/>
                        <?php
                            if (!empty($image)) {
                                ?>
                                <input type="hidden" name="hidd_image" value="<?php echo $image ?>"/>
                                <img src="<?php echo LINK_ROOT . DIR_SHARED_UPLOAD_IMAGES_DOCUMENT . $image; ?>"
                                     style="max-height: 100px;">
                            <?php
                            } ?>
                    </div>

                    <div class="uk-form-row">
                        <label for="Poster">Tài liệu <span class="required">*</span></label>

                        <input type="file" name="f_file"/>
                        <?php
                            if (!empty($file)) {
                                ?>
                                <input type="hidden" name="hidd_file" value="<?php echo $file ?>"/>
                                <?php echo $file; ?>
                            <?php
                            } ?>
                    </div>

                    <div class="uk-form-row">
                        <input type="hidden" name="Action" value="<?php echo $action; ?>">
                        <button class="uk-button uk-button-primary" id="action-button"
                                onclick="submit_data();"><?php echo $action_show; ?></button>
                        <a href="?">Hủy</a>
                    </div>

                </div>
            </fieldset>
        </form>


        <hr/>
        <h3><strong>Danh sách</strong></h3>

        <table class="uk-table uk-table-hover uk-table-striped uk-table-condensed">
            <thead>
            <tr>
                <th>Tên tài liệu</th>
                <th>Loại tài liệu</th>
                <th>Chi tiết</th>
                <th>Giá</th>
                <th>Hình ảnh</th>
                <th>Tài liệu</th>
                <th>Thao tác</th>
            </tr>
            </thead>
            <tbody>
            <?php
                $current_page = 1;
                $page_size = 10;
                if (!empty($_GET['p'])) {
                    $current_page = $_GET['p'];
                }

                $list_total = api_document::get_all_documents();
                $lib_pager = new lib_pager();
                $total_page = $lib_pager->get_total_page($list_total, $page_size);
                $current_page_list = $lib_pager->get_current_page_list($page_size, $current_page, $list_total);

                $count = 0;
                if (empty($current_page_list) || count($current_page_list) == 0) {
                    echo "<tr><td>Chưa có dữ liệu</td></tr>";
                } else {
                    foreach ($current_page_list as $i => $item) {
                        ?>
                        <tr>
                            <td><?php echo $item['doc_name'] ?></td>
                            <td><?php echo $item['doc_cat_name'] ?></td>
                            <td><?php echo $item['doc_detail'] ?></td>
                            <td><?php echo number_format($item['doc_price'], 0, '.', ',') . ' vnđ' ?></td>
                            <td>
                                <a href="<?php echo LINK_ROOT . DIR_SHARED_UPLOAD_IMAGES_DOCUMENT . $item['doc_img_url'] ?>">Hình
                                    ảnh</a></td>
                            <td><a href="<?php echo LINK_ROOT . DIR_SHARED_UPLOAD_DOCUMENTS . $item['doc_file'] ?>">Tập
                                    tin</a></td>
                            <td>
                                <a href="?Action=Edit&Id=<?php echo $item['doc_id']; ?>">
                                    <i class="uk-icon-edit"></i>
                                </a>
                                <i class="uk-icon-eraser" onclick="confirmDelete('<?php echo $item['doc_id']; ?>')"></i>
                            </td>
                        </tr>
                    <?php
                    }
                }?>
            <tr align="right" style="color:Black;background-color:#F7F7DE;">
                <td colspan="7">
                    <table>
                        <tbody>
                        <tr align="right" style="color:Black;background-color:#F7F7DE;">
                            <td colspan="7">
                                <table>
                                    <tbody>
                                    <tr>
                                        <?php for ($a = 1; $a <= $total_page; $a++) { ?>
                                            <td>
                                                                                <span>
                                                                                    <a href="?p=<?php echo $a; ?>" <?php if ($current_page != $a) echo 'style="color:Black;"'; ?>><?php echo $a; ?></a>
                                                                                </span>
                                            </td>
                                        <?php } ?>
                                    </tr>
                                    </tbody>
                                </table>
                            </td>
                        </tr>
                        </tbody>
                    </table>
                </td>
            </tr>
            </tbody>
        </table>
    </section>

    <?php
        require_once DOCUMENT_ROOT . DIR_INCLUDE . 'footer.php';
    ?>
</div>
</body>
</html>
